package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlXjlyszbjlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlyszbjlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlXjlyszbjlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlXjlyszbjlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlXjlyszbjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 细集料压碎指标记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class ReportJlXjlyszbjlbBaDetailServiceImpl implements IReportJlXjlyszbjlbBaDetailService {

    private final ReportJlXjlyszbjlbBaDetailMapper baseMapper;

    /**
     * 查询细集料压碎指标记录-详情
     */
    @Override
    public ReportJlXjlyszbjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询细集料压碎指标记录-详情列表
     */
    @Override
    public TableDataInfo<ReportJlXjlyszbjlbBaDetailVo> queryPageList(ReportJlXjlyszbjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlXjlyszbjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlXjlyszbjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询细集料压碎指标记录-详情列表
     */
    @Override
    public List<ReportJlXjlyszbjlbBaDetailVo> queryList(ReportJlXjlyszbjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlXjlyszbjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlXjlyszbjlbBaDetail> buildQueryWrapper(ReportJlXjlyszbjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlXjlyszbjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlXjlyszbjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShaiklj()), ReportJlXjlyszbjlbBaDetail::getShaiklj, bo.getShaiklj());
        lqw.eq(bo.getZhilmg() != null, ReportJlXjlyszbjlbBaDetail::getZhilmg, bo.getZhilmg());
        lqw.eq(bo.getShaiyl() != null, ReportJlXjlyszbjlbBaDetail::getShaiyl, bo.getShaiyl());
        lqw.eq(bo.getTonggl() != null, ReportJlXjlyszbjlbBaDetail::getTonggl, bo.getTonggl());
        lqw.eq(bo.getYaszb() != null, ReportJlXjlyszbjlbBaDetail::getYaszb, bo.getYaszb());
        lqw.eq(bo.getLijPjz() != null, ReportJlXjlyszbjlbBaDetail::getLijPjz, bo.getLijPjz());
        lqw.eq(bo.getXijlYszb() != null, ReportJlXjlyszbjlbBaDetail::getXijlYszb, bo.getXijlYszb());
        return lqw;
    }

    /**
     * 新增细集料压碎指标记录-详情
     */
    @Override
    public Boolean insertByBo(ReportJlXjlyszbjlbBaDetailBo bo) {
        ReportJlXjlyszbjlbBaDetail add = BeanUtil.toBean(bo, ReportJlXjlyszbjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改细集料压碎指标记录-详情
     */
    @Override
    public Boolean updateByBo(ReportJlXjlyszbjlbBaDetailBo bo) {
        ReportJlXjlyszbjlbBaDetail update = BeanUtil.toBean(bo, ReportJlXjlyszbjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlXjlyszbjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除细集料压碎指标记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
